package easy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:ZouHeng
 * Description:
 * Date:2021-10-09  17:17
 **/
public class EasySolution {

    /**
     * 1、两数之和
     */
    public int[] twoSum_my(int[] nums, int target) {
        int[] result = new int[2];
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 1、两数之和
     * 利用hash表，直接查找target-x
     */
    public int[] twoSum_best(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }

    /**
     * 7、整数反转
     */
    public int reverse_my(int x) {
        List<Integer> list = new ArrayList<>();
        while (x != 0) {
            int num = (int) (x%Math.pow(10, 1));
            list.add(num);
            x = x/(int)Math.pow(10, 1);
        }

        double result = 0;
        for (int i = 0; i < list.size(); i++) {
            if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){
                return 0;
            }
            result += list.get(i)*Math.pow(10, list.size() - i - 1);
        }
        return (int)result;
    }

    /**
     * 7、整数反转
     */
    public int reverse_best(int x) {
        int rev = 0;
        while (x != 0) {
            if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) {
                return 0;
            }
            int digit = x % 10;
            x /= 10;
            rev = rev * 10 + digit;
        }
        return rev;
    }

    public static void main(String[] args) {
        System.out.println((int)2*Math.pow(2,31));
    }
}
